import apiClient from "@/api/apiClient";

export interface SystemUserItem {
	id: number;
	username: string;
	nickname: string;
	email: string;
	mobile: string;
	avatar: string;
	status: number;
	createtime: number;
	updatetime: number;
	role_id: number;
	role: {
		id: number;
		name: string;
		code: string;
	};
}

export interface SystemUserListParams {
	page: number;
	limit: number;
	username?: string;
	status?: number;
}

export interface SystemUserAddParams {
	username: string;
	password: string;
	nickname: string;
	email: string;
	mobile: string;
	avatar?: string;
	role_id: number;
	status: number;
}

export interface SystemUserEditParams {
	id: number;
	nickname?: string;
	email?: string;
	mobile?: string;
	avatar?: string;
	role_id?: number;
	status?: number;
}

export interface ChangePasswordParams {
	id: number;
	oldpassword: string;
	newpassword: string;
}

export enum SystemUserApi {
	UserList = "/auth/admin/index",
	UserAdd = "/auth/admin/add",
	UserEdit = "/auth/admin/edit",
	UserDelete = "/auth/admin/del",
	UserChangePwd = "/auth/admin/changepwd",
	UserInfo = "/auth/admin/info",
}

export default {
	userList: (params: SystemUserListParams) =>
		apiClient.get<{ total: number; items: SystemUserItem[] }>({
			url: SystemUserApi.UserList,
			params,
		}),

	userAdd: (data: SystemUserAddParams) =>
		apiClient.post({
			url: SystemUserApi.UserAdd,
			data,
		}),

	userEdit: (data: SystemUserEditParams) =>
		apiClient.post({
			url: SystemUserApi.UserEdit,
			data,
		}),

	userDelete: (id: number) =>
		apiClient.post({
			url: SystemUserApi.UserDelete,
			data: { id },
		}),

	changePassword: (data: ChangePasswordParams) =>
		apiClient.post({
			url: SystemUserApi.UserChangePwd,
			data,
		}),

	userInfo: (id: number) =>
		apiClient.get<SystemUserItem>({
			url: SystemUserApi.UserInfo,
			params: { id },
		}),
};
